/*
 * SortedListModelDemo.java
 *
 * Copyright 2006 Sun Microsystems, Inc. ALL RIGHTS RESERVED Use of 
 * this software is authorized pursuant to the terms of the license 
 * found at http://developers.sun.com/berkeley_license.html .
 *
 */


import gui.cliente.SortedListModel;
import gui.cliente.SortedListModel.SortOrder;

import java.awt.event.KeyEvent;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ListModel;



/**
 *
 * @author  John O'Conner
 */
public class SortedListModelDemo extends javax.swing.JFrame {
    
    /**
     * Creates new form SortedListModelDemo
     */
    public SortedListModelDemo() {
        super("Sorted ListModel Demo");
        unsortedModel = new DefaultListModel();
        sortedModel = new SortedListModel(unsortedModel);
        initComponents();
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        bgSortOrder = new javax.swing.ButtonGroup();
        txtAddEntry = new javax.swing.JTextField();
        btnAddEntry = new javax.swing.JButton();
        scrollPane = new javax.swing.JScrollPane();
        list = new javax.swing.JList();
        btnDeleteSelection = new javax.swing.JButton();
        panelSort = new javax.swing.JPanel();
        rbAscendingSort = new javax.swing.JRadioButton();
        rbDescendingSort = new javax.swing.JRadioButton();
        rbUnordered = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        txtAddEntry.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtAddEntryKeyPressed(evt);
            }
        });

        btnAddEntry.setText("Add");
        btnAddEntry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddEntryActionPerformed(evt);
            }
        });

        scrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder("JList with SortedListModel"));
        scrollPane.setMinimumSize(new java.awt.Dimension(0, 0));
        scrollPane.setPreferredSize(new java.awt.Dimension(250, 150));
        list.setModel(sortedModel);
        list.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                listKeyPressed(evt);
            }
        });
        list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listValueChanged(evt);
            }
        });

        scrollPane.setViewportView(list);

        btnDeleteSelection.setText("Delete");
        btnDeleteSelection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteSelectionActionPerformed(evt);
            }
        });

        panelSort.setBorder(javax.swing.BorderFactory.createTitledBorder("Sort Order"));
        bgSortOrder.add(rbAscendingSort);
        rbAscendingSort.setSelected(true);
        rbAscendingSort.setText("Ascending");
        rbAscendingSort.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbAscendingSort.setMargin(new java.awt.Insets(0, 0, 0, 0));
        rbAscendingSort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbAscendingSortActionPerformed(evt);
            }
        });

        bgSortOrder.add(rbDescendingSort);
        rbDescendingSort.setText("Descending");
        rbDescendingSort.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbDescendingSort.setMargin(new java.awt.Insets(0, 0, 0, 0));
        rbDescendingSort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbDescendingSortActionPerformed(evt);
            }
        });

        bgSortOrder.add(rbUnordered);
        rbUnordered.setText("Unordered");
        rbUnordered.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbUnordered.setMargin(new java.awt.Insets(0, 0, 0, 0));
        rbUnordered.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbUnorderedActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout panelSortLayout = new org.jdesktop.layout.GroupLayout(panelSort);
        panelSort.setLayout(panelSortLayout);
        panelSortLayout.setHorizontalGroup(
            panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelSortLayout.createSequentialGroup()
                .add(panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(rbAscendingSort)
                    .add(rbDescendingSort)
                    .add(rbUnordered))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelSortLayout.setVerticalGroup(
            panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelSortLayout.createSequentialGroup()
                .add(rbAscendingSort)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(rbDescendingSort)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(rbUnordered))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, txtAddEntry, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, btnAddEntry, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, btnDeleteSelection, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(txtAddEntry, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnAddEntry))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnDeleteSelection)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void listValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listValueChanged
        int sortedFirstIndex = evt.getFirstIndex();
        int sortedLastIndex = evt.getLastIndex();
        for (int sortedIndex = sortedFirstIndex; sortedIndex <= sortedLastIndex; sortedIndex++) {
            int unsortedIndex = sortedModel.toUnsortedModelIndex(sortedIndex);
            // do something with unsorted model index
            System.out.printf("listValueChanged index: %d (sorted) %d (unsorted)\n", 
                    sortedIndex, unsortedIndex);
        }
    }//GEN-LAST:event_listValueChanged

    private void listKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_listKeyPressed
        int key = evt.getKeyCode();
        if (key == evt.VK_DELETE) {
            int option = JOptionPane.showConfirmDialog(this, "Delete selected elements?");
            if (option == JOptionPane.YES_OPTION) {
                btnDeleteSelectionActionPerformed(null);
            }
        }
    }//GEN-LAST:event_listKeyPressed

    private void rbUnorderedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbUnorderedActionPerformed
        sortedModel.setSortOrder(SortOrder.UNORDERED);
    }//GEN-LAST:event_rbUnorderedActionPerformed

    private void rbDescendingSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbDescendingSortActionPerformed
        sortedModel.setSortOrder(SortOrder.DESCENDING);
    }//GEN-LAST:event_rbDescendingSortActionPerformed

    private void rbAscendingSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbAscendingSortActionPerformed
        sortedModel.setSortOrder(SortOrder.ASCENDING);
        
    }//GEN-LAST:event_rbAscendingSortActionPerformed

    private void btnDeleteSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteSelectionActionPerformed
        int[] sortedSelection = list.getSelectedIndices();
        int[] unsortedSelection = sortedModel.toUnsortedModelIndices(sortedSelection);
        
        for (int x = unsortedSelection.length - 1; x >= 0; --x) {
            unsortedModel.remove(unsortedSelection[x]);
        }
    }//GEN-LAST:event_btnDeleteSelectionActionPerformed

    private void txtAddEntryKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAddEntryKeyPressed
        int keyCode = evt.getKeyCode();
        if (keyCode == KeyEvent.VK_ENTER) {
                btnAddEntryActionPerformed(null);
        }
        // else ignore key presses
    }//GEN-LAST:event_txtAddEntryKeyPressed

    private void btnAddEntryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddEntryActionPerformed
        String newText = txtAddEntry.getText();
        // don't add empty or null strings to model
        if (newText != null && newText.length() > 0) {
            unsortedModel.addElement(newText);
        }
        txtAddEntry.setText(null);
    }//GEN-LAST:event_btnAddEntryActionPerformed
    
    void test1() {
        String[] list = {"turtle", "frog", "anteater", 
                         "ant", "rabbit", "dog", "cat" };
        for (String data : list ) {
            unsortedModel.addElement(data);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        SortedListModelDemo app = new SortedListModelDemo();
        app.setVisible(true);
        app.test1();
    }

    
    private SortedListModel sortedModel;
    private DefaultListModel unsortedModel;
    

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bgSortOrder;
    private javax.swing.JButton btnAddEntry;
    private javax.swing.JButton btnDeleteSelection;
    private javax.swing.JList list;
    private javax.swing.JPanel panelSort;
    private javax.swing.JRadioButton rbAscendingSort;
    private javax.swing.JRadioButton rbDescendingSort;
    private javax.swing.JRadioButton rbUnordered;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextField txtAddEntry;
    // End of variables declaration//GEN-END:variables
    
}
